<template>
	<view class="content">

		<view class="" v-if="id == 1">

			<view class="name">{{name}}</view>
			<view class="type">类型选择</view>

			<view class="kuang">
				<view class="one" v-for="(item, idx) in list" v-if="item.name != '会员去世'">
					<view @tap="to_apply(item.id, item.name)">
						<image class='img' src="@/static/activity/apply/new/hyww.png" mode=""></image>
						<view class="zi">{{item.name}}</view>
					</view>
				</view>
			</view>
		</view>

		<view class="" v-if="id == 49">

			<!-- <view class="name1">{{name}}</view> -->

			<view style="height: 210rpx;"></view>

			<view class="tuichu">

				<!-- 互助金生效时间： 缴费金额够1w时，最后一笔金额缴费时间+1天 -->
				<view class="name1">{{name}}</view>
				<view class="gui-form-item gui-border-b">
					<text class="gui-form-label">职工姓名 :</text>
					<view class="gui-form-body beijing">
						<input disabled v-model="user.chinaname" value="111" type="text" class="gui-form-input"
							placeholder="职工姓名" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b">
					<text class="gui-form-label" style="width: 105rpx;">厂牌号 :</text>
					<view class="gui-form-body beijing">
						<input disabled v-model="user.lg_id" value="" type="text" class="gui-form-input"
							placeholder="厂牌号" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b">
					<text class="gui-form-label" style="width: 105rpx;">现单位 :</text>
					<view class="gui-form-body beijing">
						<input disabled v-model="user.organization" value="" type="text" class="gui-form-input"
							placeholder="现单位" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b">
					<text class="gui-form-label" style="width: 220rpx;">已缴互助金金额 :</text>
					<view class="gui-form-body beijing">
						<input disabled v-model="hzj_sum" value="" type="text" class="gui-form-input"
							placeholder="已缴互助金金额" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b">
					<text class="gui-form-label" style="width: 185rpx;">是否足额缴纳 :</text>
					<view class="gui-form-body beijing">
						<input disabled v-model="shifou" value="" type="text" class="gui-form-input"
							placeholder="缴费金额够1w：是；不够：否 :" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b" v-if="lishi.length > 0 && lishi[0].lessee_name != null">
					<text class="gui-form-label" style="width: 275rpx;">缴费完成时所在单位 :</text>
					<view class="gui-form-body beijing">
						<input v-if="shifou == '是'" disabled v-model="lishi[0].lessee_name.name" value="" type="text"
							class="gui-form-input" placeholder="缴费完成时所在单位" />
						<input v-else disabled value="" type="text" class="gui-form-input" placeholder="未足额缴费" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b" v-if="lishi.length > 0">
					<text class="gui-form-label" style="width: 225rpx;">互助金生效时间:</text>
					<view class="gui-form-body beijing">
						<input v-if="shifou == '是'" disabled v-model="lishi[0].pay_time" value="" type="text"
							class="gui-form-input" placeholder="缴费金额够1w时，最后一笔金额缴费时间+1天" />
						<input v-else disabled value="" type="text" class="gui-form-input" placeholder="未足额缴费" />
					</view>
				</view>

				<view class="gui-form-item gui-border-b">
					<text class="gui-form-label" style="width: 275rpx;">互助金缴纳历史信息 :</text>
					<view @click="chakan" class="gui-form-body gui-bg-white"
						style="font-size: 30rpx; height: 75rpx; line-height: 75rpx;">
						点我查看</view>
				</view>

			</view>

			<!-- hzj_sum -->
			<view class="shenqing" v-if="whether" @click="to_apply1(id, name)">
				<button class="btn">我要申请</button>
			</view>

		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				name: '',
				user: [],
				list: [],
				shuju: [],
				last: [],
				lishi: [],
				hzj_sum: null,
				shifou: null,
				whether: false
			}
		},
		onLoad(option) {
			this.user = uni.getStorageSync("member");
			// console.log('user: ', this.user);
			this.id = option.category
			this.name = option.cate_name
			uni.showLoading({
				title: '加载中'
			});
			if (this.id == 1) {
				this.get_category();
			} else if (this.id == 49) {
				this.lishijilu()
			}
		},
		methods: {
			chakan() {
				if (this.lishi.length <= 0) {
					uni.showToast({
						title: '无缴费记录',
						duration: 2000,
						icon: 'error'
					});
				} else {
					uni.navigateTo({
						url: 'mutual_aid_money'
					})
				}
			},
			zuo() {
				uni.navigateBack();
			},
			//获取分类
			get_category() {
				let self = this;
				let url = '/hzj_apply/get_category'
				let parm = {
					c_id: self.id
				}
				self.$boya.NPost(url, parm, function(res) {
					uni.hideLoading();
					console.log('cate: ', res.data.cate);
					let data = res.data.cate
					for (var i = 0; i < data.length; i++) {
						//  && data[i].name != '会员结婚'
						if (data[i].name != '会员去世') {
							self.list.push(data[i])
						}
					}
					// self.list = res.data.cate
					console.log('list: ', self.list);
				})
			},
			lishijilu() {
				let self = this
				// 获取当前日期
				let today = new Date();
				// 获取年份
				let year = today.getFullYear();
				// 获取月份（注意月份是从0开始计数的，所以要加1）
				let month = today.getMonth() + 1;
				// 获取日期
				let day = today.getDate();
				let demo2Val = year + '/' + month + '/' + day

				let url = '/hzj/get_order'
				let parm = {
					name: self.user.chinaname,
					pages: 1,
					price: 0,
					limit: 10000,
					sele_fee: null,
					uuid: self.user.uuid,
					time: {
						star_time: '2018/01/01',
						end_time: demo2Val
					},
					// token: '14d78037-08e3-49bf-b4ec-b8da61979af1'
					token: uni.getStorageSync("ttsxAuthToken")
				}
				self.$boya.NPost(url, parm, function(res) {
					console.log('res: ', res);
					uni.hideLoading();
					if (res.data.hzj_sum) {
						self.hzj_sum = res.data.hzj_sum
						self.whether = true
					} else {
						self.hzj_sum = 0
					}
					if (self.hzj_sum >= 10000) {
						self.shifou = '是'
					} else {
						self.shifou = '否'
					}
					let shuju = res.data.data
					for (var i = 0; i < shuju.length; i++) {
						if (shuju[i].fee_items == '医疗互助金') {
							self.lishi.push(shuju[i])
						}
					}
					console.log('lishi: ', self.lishi);
					
					if (self.lishi[0].pay_time) {
						let dateStr = self.lishi[0].pay_time;
						let timestamp = new Date(dateStr).getTime();
						// 将时间戳增加一天（24小时）
						timestamp += 24 * 60 * 60 * 1000;
						// 创建新的Date对象，并设置为增加一天后的日期
						let newDate = new Date(timestamp);
						// 获取新日期的年、月、日
						let year = newDate.getFullYear();
						let month = newDate.getMonth() + 1; // 注意月份是从0开始计数的，所以要+1
						let day = newDate.getDate();
						// 拼接新日期的字符串
						self.lishi[0].pay_time =
							`${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
					}
				})
			},
			to_apply(id, name) {
				var self = this
				uni.showLoading({
					title: '加载中...'
				});
				let url = '/hzj_apply/get_apply'
				let parm = {
					pages: 0,
					user_id: self.user.id,
					apply_id: 0,
					switch_index: 0
				}
				self.$boya.NPost(url, parm, function(res) {
					console.log('申请单: ', res.data);
					let data = res.data
					self.shuju = []
					for (var i = 0; i < data.length; i++) {
						if (id == data[i].category_id) {
							if (data[i].status == 10) {
								self.shuju.push(data[i])
							}
						}
					}
					if (self.shuju.length != 0) {
					
						// 获取当天日期后，获取当天时间存在的年份
						let now = new Date().getFullYear();
						self.last = []
					
						for (var j = 0; j < self.shuju.length; j++) {
					
							// 一年一次
							if (self.shuju[0].category_id == 3 || self.shuju[0].category_id == 5) {
								// 获取当前类型所有数据的创建时间的时间戳
								let created_at = Date.parse(self.shuju[j].created_at);
								// 时间格式化
								let shijianchuo = new Date(parseInt(created_at));
								// 获取数据创建日期后，获取数据创建日期的年份
								let shijian = shijianchuo.getFullYear()
								// console.log(shijian);
								self.last.push(shijian)
					
								// 方法一：
								// const value = now;
								// if (self.last.includes(value)) {
								// 	uni.showModal({
								// 		title: '该类型一年只可申报一次',
								// 		content: '您最少有一条申请在受理中或已通过',
								// 		success(res) {
								// 			uni.navigateTo({
								// 				url: './my_apply'
								// 			});
								// 		}
								// 	})
								// }
					
								// 方法二：
								let yici = []
								for (var l = 0; l < self.last.length; l++) {
									if (self.last[l] == now) {
										yici.push(self.last[l])
									}
								}
					
								if (yici.length >= 1) {
									uni.showModal({
										title: '该类型一年只可申报一次',
										content: '您最少有一条申请在受理中或已通过',
										success(res) {
											uni.navigateTo({
												url: './my_apply'
											});
										}
									})
								} else {
									uni.showModal({
										title: '提示',
										content: '是否报备申请',
										success(res) {
											if (res.confirm) {
												uni.navigateTo({
													url: './apply?category=' + self.id +
														'&cate_name=' + self.name + '&name=' +
														name + '&category_id=' + id
												});
											} else {
												uni.navigateTo({
													url: './my_apply'
												});
											}
										}
									})
								}
							}
							// else if(self.shuju[0].category_id == 4) {
							// 	uni.showToast({
							// 		title: '维护中,请稍后再试',
							// 		duration: 2000,
							// 		icon: 'error'
							// 	});
							// }
							// 一年两次
							else if (self.shuju[0].category_id == 4 || self.shuju[0].category_id == 6) {
					
								// 获取当前类型所有数据的创建时间的时间戳
								let created_at = Date.parse(self.shuju[j].created_at);
								// 时间格式化
								let shijianchuo = new Date(parseInt(created_at));
								// 获取数据创建日期后，获取数据创建日期的年份
								let shijian = shijianchuo.getFullYear()
								// console.log(shijian);
								self.last.push(shijian)
					
								let liangci = []
								for (var k = 0; k < self.last.length; k++) {
									if (self.last[k] == now) {
										liangci.push(self.last[k])
									}
								}
					
								if (liangci.length >= 2) {
									uni.showModal({
										title: '该类型一年只可申报两次',
										content: '您最少有两条申请在受理中或已通过',
										success(res) {
											uni.navigateTo({
												url: './my_apply'
											});
										}
									})
								} else {
									uni.showModal({
										title: '提示',
										content: '是否报备申请',
										success(res) {
											if (res.confirm) {
												uni.navigateTo({
													url: './apply?category=' + self.id +
														'&cate_name=' + self.name + '&name=' +
														name + '&category_id=' + id
												});
											} else {
												uni.navigateTo({
													url: './my_apply'
												});
											}
										}
									})
								}
							}
						}
					} else {
						// if (id == 4) {
						// 	uni.showToast({
						// 		title: '维护中,请稍后再试',
						// 		duration: 2000,
						// 		icon: 'error'
						// 	});
						// } else {
						uni.showModal({
							title: '提示',
							content: '是否报备申请',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: './apply?category=' +
											self.id + '&cate_name=' +
											self.name + '&name=' +
											name + '&category_id=' + id
									});
								} else {
									uni.navigateTo({
										url: './my_apply'
									});
								}
							}
						})
						// }
					}
					uni.hideLoading();
				})
			},
			to_apply1(id, name) {
				var self = this
				let url = '/hzj_apply/get_apply'
				let parm = {
					pages: 0,
					user_id: self.user.id,
					apply_id: 0,
					switch_index: 0
				}
				self.$boya.NPost(url, parm, function(res) {
					uni.showModal({
						title: '提示',
						content: '是否报备申请',
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: './apply?category=' + id +
										'&cate_name=' +
										name
								});
							} else {
								uni.navigateTo({
									url: './my_apply'
								});
							}
						}
					})
				})
			},
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background: url('https://boyaltd.oss-cn-hangzhou.aliyuncs.com/2022-08/05/XeIqBphptSm0SK1659691416220805.jpg') 100% 100%/100% 100%;
	}

	.name {
		font-size: 55rpx;
		font-weight: bold;
		text-align: center;
		padding-top: 550rpx;
	}

	.name1 {
		font-size: 55rpx;
		padding-bottom: 50rpx;
		font-weight: bold;
		text-align: center;
	}

	.type {
		font-size: 35rpx;
		font-weight: bold;
		text-align: center;
		margin-top: 30rpx;
	}

	.kuang {
		width: 85%;
		margin: 0 auto;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
		margin-top: 50rpx;
		font-size: 30rpx;
		font-weight: bold;
		text-align: center;
	}

	.one {
		width: 200rpx;
		height: 250rpx;
		padding: 10rpx;
		margin-top: 40rpx;
		background: url('../../../../static/activity/apply/new/bj.png') 100% 100%/ 100% 100%;
	}

	.img {
		width: 120rpx;
		height: 120rpx;
		margin-top: 20rpx;
	}

	.zi {
		padding: 10rpx;
		text-align: center;
		margin-right: 9rpx;
		margin-top: 10rpx;
	}

	.tuichu {
		width: 86%;
		margin: 0 auto;
		margin-top: 50rpx;
		padding: 18rpx;
		background-color: rgba(255, 255, 255, 0.8);
		border-radius: 15rpx;
	}

	.btn {
		width: 280rpx;
		margin: 0 auto;
		margin-top: 50rpx;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 35rpx;
		font-weight: bold;
		background-color: white;
	}
	
	.beijing {
		font-weight: bold;
		padding-left: 10rpx;
	}
</style>